package cn.madis.ymcc.controller;


import cn.madis.ymcc.doc.CourseDoc;
import cn.madis.ymcc.dto.SearchEsDto;
import cn.madis.ymcc.result.JSONResult;
import cn.madis.ymcc.result.PageList;
import cn.madis.ymcc.service.ICourseSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/search")
public class CourseSearchController {
    @Autowired
    private ICourseSearchService searchService;

    /**
     * 课程信息保存到es中
     */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    @PreAuthorize("hasAnyAuthority('es:saveCourse')")
    public JSONResult save(@RequestBody CourseDoc courseDoc){
        searchService.save(courseDoc);
        return JSONResult.success();
    }

    /**
     * 删除课程信息es
     */
    @RequestMapping(value="/remove/{courseId}",method= RequestMethod.POST)
    public JSONResult remove(@PathVariable Long courseId){
        searchService.remove(courseId);
        return JSONResult.success();
    }

    /**
     * 查es中信息
     */
    @RequestMapping(value="/search",method= RequestMethod.POST)
    public JSONResult search(@RequestBody SearchEsDto searchEsDto){
       PageList<CourseDoc> page = searchService.search(searchEsDto);
        return JSONResult.success(page);
    }
}
